package cn.dyw.ui;

import cn.dyw.player.*;
import cn.dyw.ui.component.CircleButton;
import cn.dyw.ui.component.MenuOperation;
import javazoom.jl.player.advanced.PlaybackEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MainFrame {


    private MainPanel mainPanel;

    public MainFrame() {
        init();
    }

    /**
     * 初始化所组件
     */
    private void init() {
        mainPanel = new MainPanel();

        Thread playerThread = new Thread(() -> {
            Player player = PlayerFactory.factory();
            player.setListener(new PlaybackListener() {
                @Override
                public void playbackStarted(AudioFileInfo info) {
                    mainPanel.getMusicTitle().setText(info.getTitle());
                    mainPanel.getMusicTitle().setSize(300, 20);
                    BufferedImage icon = info.getIcon();
                    try {
                        mainPanel.getMusicImage()
                                .setIcon(
                                        MainPanel.createIcon(
                                                new ImageIcon(ImageUtils.mergeImage(icon)
                                                ), 300, 300)
                                );
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void playbackFinished(PlaybackEvent evt) {

                }

                @Override
                public void playbackStop(PlaybackEvent evt) {

                }

                @Override
                public void playbackProgress(int position) {
                    mainPanel.getMusicProgress().setValue(position);
                }
            });
            try {
                player.suspend();
                player.play(Integer.MAX_VALUE);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(2);
            }
        });
        playerThread.setName("player-thread");
        playerThread.start();

        Thread UIThread = new Thread(() -> {

            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame jFrame = new JFrame("java 音乐播放器");
            jFrame.setJMenuBar(new MenuOperation(mainPanel));
            jFrame.getContentPane().add(mainPanel.getMusicPanel());

            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setResizable(false);
            jFrame.setSize(300, 450);
            jFrame.setVisible(true);

            // 必须在视图渲染好之后设置按钮的大小
            ((CircleButton) mainPanel.getNext()).setSize(30);
            ((CircleButton) mainPanel.getPrevious()).setSize(30);
            ((CircleButton) mainPanel.getStop()).setSize(30);

            jFrame.addWindowListener(new WindowAdapter() {
                /**
                 * Invoked when a window has been closed.
                 *
                 * @param e
                 */
                @Override
                public void windowClosed(WindowEvent e) {
                    Player player = PlayerFactory.factory();
                    player.close();
                }
            });
        });

        UIThread.setName("ui-thread");
        UIThread.start();
    }
}
